﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jemer.Domain;
using Jemer.Data;
using Jemer.Data.Infrastructure;
using Jemer.Domain.Entities;
using Jemer.Data.Repositories;

namespace Jemer.Service
{

    public interface INewsInfoService
    {
        IQueryable<NewsInfo> GetNewsInfoes();
        NewsInfo GetNewsInfo(int id);
        void CreateNewsInfo(NewsInfo newsInfo);
        void DeleteNewsInfo(int id);
        void SaveNewsInfo();
    }

    public class NewsInfoService : INewsInfoService
    {
        private readonly INewsInfoRepository newsInfoRepository;
        private readonly IUnitOfWork unitOfWork;
        public NewsInfoService(INewsInfoRepository newsInfoRepository, IUnitOfWork unitOfWork)
        {
            this.newsInfoRepository = newsInfoRepository;
            this.unitOfWork = unitOfWork;
        }
        #region INewsInfoService Members

        public IQueryable<NewsInfo> GetNewsInfoes()
        {
            var newsInfoes = newsInfoRepository.GetAll();
            return newsInfoes;
        }

        public NewsInfo GetNewsInfo(int id)
        {
            var newsInfo = newsInfoRepository.GetById(id);
            return newsInfo;
        }

        public void CreateNewsInfo(NewsInfo newsInfo)
        {
            newsInfoRepository.Add(newsInfo);
            SaveNewsInfo();
        }

        public void DeleteNewsInfo(int id)
        {
            var newsInfo = newsInfoRepository.GetById(id);
            newsInfoRepository.Delete(newsInfo);
            SaveNewsInfo();
        }

        public void SaveNewsInfo()
        {
            unitOfWork.Commit();
        }

        #endregion
    }


}